package edu.hunau.cxb19.oop.class24;

import java.util.Optional;

import org.junit.Test;

/**
 *
 *@author hb
 *
 * 2020-03-27 16:00:59
 */
public class ReduceTest {

	@Test
	public void sumTest() {
		Integer sum = Apple.getDatas().stream()
						                .map(Apple::getWeight)
						                .reduce(0, (a,b)-> a+b);
		System.out.println(sum);
	}
	
	@Test
	public void sumTest02() {
		Optional<Integer> optional = Apple.getDatas().stream()
						                .map(Apple::getWeight)
						                .reduce((a,b)-> a+b);
	    if(optional.isPresent()) {
	    	System.out.println(optional.get());
	    }	
	}
	
	@Test
	public void maxTest() {
		Optional<Integer> optional = Apple.getDatas().stream()
									                .map(Apple::getWeight)
									                .reduce((x,y)->x>y?x:y);
									                //.reduce(Integer::max); //.reduce((a,b)->Integer.max(a,b));
		optional.ifPresent(System.out::println);
	}
	
	@Test
	public void countTest() {
		//统计苹果的数量
		long count = Apple.getDatas().stream().count();
		
		int nums = Apple.getDatas().stream().map(a->1).reduce(0, (a,b)->a+b);
		System.out.println(count+" : "+nums);
	}
}
